/**
 * @title TimeUtil.java
 * @package com.esun.util
 * @description 
 * @author liugan
 * @date 2012-4-26
 * @copyright 深圳市易讯天空
 */
package com.esun.util;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.text.TextUtils;
import android.util.Log;

/**
 * @title TimeUtil
 * @description 远程时间管理类
 * @author liugan
 */
public class TimeUtil {

	public final static Format sameDayFormat = new SimpleDateFormat("yyyyMMdd");
	public final static SimpleDateFormat calendarFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	private static TimeUtil instance = new TimeUtil();
	private Calendar mCalendar;
	private String mDay;

	private TimeUtil() {
		mCalendar = Calendar.getInstance();
		mDay = sameDayFormat.format(mCalendar.getTime());
	}

	public static TimeUtil getInstance() {
		return instance;
	}

	public synchronized boolean checkForRemoteDateChange(String sTime) {
		boolean flag = false;
		try {
			if (!TextUtils.isEmpty(sTime)) {
				final Date latestRemoteTime = calendarFormat.parse(sTime);
				final String lastestRemoteDay = sameDayFormat
						.format(latestRemoteTime);
				if (!mDay.equals(lastestRemoteDay)) {
					// if (MatchListManager.getInstance().isChooseToday()) {
					// MatchListManager.getInstance().getCalendar()
					// .setTime(latestRemoteTime);
					// }

					flag = true;
				}
				mCalendar.setTime(latestRemoteTime);
				mDay = lastestRemoteDay;
			}
			return flag;
		} catch (ParseException e) {
			Log.e("TimeUtil", "parse fail");
			return false;
		}
	}

	/**
	 * @title isToday
	 * @description 更具最近保存的服务器时间判断是非是今天
	 * @param chosenDay
	 * @return
	 */
	public synchronized boolean isToday(String chosenDay) {
		if (mDay.equals(chosenDay)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @title getLatestRemoteTime
	 * @description 获取最近保存的服务器时间
	 * @return
	 */
	public synchronized Date getLatestRemoteTime() {
		return mCalendar.getTime();
	}

	public synchronized Calendar getCalendar() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getLatestRemoteTime());
		return calendar;
	}

	/**
	 * @title isCurrentMonth
	 * @description 判断是否是当前月份
	 * @param year
	 * @param month
	 */
	public synchronized boolean isCurrentMonth(int year, int month) {
		boolean flag = false;
		if (mCalendar.get(Calendar.YEAR) == year
				&& mCalendar.get(Calendar.MONTH) == month) {
			flag = true;
		}
		return flag;
	}
}
